package com.yami.shop.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.cashregisterapp.AppCashRegister;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.AppCashRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;

@Controller
@RequestMapping("/app/appCashRegister")
public class AppCashRegisterController {

    @Autowired
    private AppCashRegisterService appCashRegisterService;


    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('app:appCashRegister:page')")
    public ResponseEntity<IPage<AppCashRegister>> page(AppCashRegister appCashRegister, PageParam<AppCashRegister> page) {
        IPage<AppCashRegister> userIPage = appCashRegisterService.page(page, new LambdaQueryWrapper<AppCashRegister>()
                .like(StrUtil.isNotBlank(appCashRegister.getBrandName()), AppCashRegister::getBrandName, appCashRegister.getBrandName())
                .isNotNull(AppCashRegister::getBrandName)
                .orderByDesc(AppCashRegister::getCreateTime));
        return ResponseEntity.ok(userIPage);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{cashRegisterId}")
    @PreAuthorize("@pms.hasPermission('app:appCashRegister:info')")
    public ResponseEntity<AppCashRegister> info(@PathVariable("cashRegisterId") Long cashRegisterId) {
        AppCashRegister appCashRegister = appCashRegisterService.getById(cashRegisterId);
        return ResponseEntity.ok(appCashRegister);
    }


    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('app:appCashRegister:update')")
    public ResponseEntity<Void> update(@RequestBody AppCashRegister appCashRegister ) {
        appCashRegister.setUpdateTime(new Date());
        appCashRegisterService.updateById(appCashRegister);
        return ResponseEntity.ok().build();
    }


    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('app:appCashRegister:delete')")
    public ResponseEntity<Void> delete(@RequestBody String[] userIds) {
        appCashRegisterService.removeByIds(Arrays.asList(userIds));
        return ResponseEntity.ok().build();
    }

    /**
     * 新增
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('app:appCashRegister:save')")
    public ResponseEntity<Boolean> save(@RequestBody @Valid AppCashRegister appCashRegister) {
        return ResponseEntity.ok(appCashRegisterService.save(appCashRegister));
    }


}
